package the.lb.workbench.hlk.controller;

import org.springframework.web.bind.annotation.*;
import org.wisteria.framework.web.APIResult;
import org.wisteria.framework.web.QueryResult;
import org.wisteria.framework.web.context.UserContext;
import the.lb.workbench.hlk.dto.VanCondition;
import the.lb.workbench.hlk.model.Van;
import the.lb.workbench.hlk.service.IVanService;
import the.lb.workbench.hlk.vo.CourierVO;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/hlk/van")
public class VanController {

    @Resource
    private IVanService vanService;

    @PostMapping("/add")
    public APIResult add(
            String vanNumber
    ) {
        Van entity = new Van();
        entity.setVanNumber(vanNumber);
        entity.setCreatedId(UserContext.userId());
        entity.setCreatedName(UserContext.userName());
        entity.setCreatedTime(new Date());
        vanService.save(entity);
        return APIResult.success();
    }

    @PostMapping("/remove")
    public APIResult remove(@RequestParam(value = "ids") List<String> ids) {
        vanService.removeByIds(ids, UserContext.userId(), UserContext.userName());
        return APIResult.success();
    }

    @PostMapping("/modify")
    public APIResult modify(
            @RequestParam(value = "vanId", required = true) String vanId,
            @RequestParam(value = "vanNumber", required = true) String vanNumber
    ) {
        Van entity = new Van();
        entity.setVanId(vanId);
        entity.setVanNumber(vanNumber);
        entity.setModifiedId(UserContext.userId());
        entity.setModifiedName(UserContext.userName());
        entity.setModifiedTime(new Date());
        vanService.updateById(entity);
        return APIResult.success();
    }

    @GetMapping("/{vanId}")
    public APIResult get(@PathVariable(value = "vanId") String vanId) {
        return APIResult.success(vanService.getById(vanId));
    }

    @GetMapping("/page")
    public APIResult<QueryResult<CourierVO>> page(
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "pageNum") Integer pageNum,
            @RequestParam(value = "pageSize") Integer pageSize
    ) {
        VanCondition condition = new VanCondition();
        condition.setKeyword(keyword);
        return APIResult.success(vanService.pageList(condition, pageNum, pageSize));
    }
}
